package com.dkd.interfaceinfo.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.dkd.common.core.domain.entity.SysUser;
import com.dkd.common.utils.SecurityUtils;
import com.dkd.interfaceinfo.domain.InterfaceInfoInvoke;
import com.dkd.system.service.impl.SysUserServiceImpl;
import com.dkd.ypclientsdk.client.YpApiClient;
import com.dkd.ypclientsdk.model.User;
import com.google.gson.Gson;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.interfaceinfo.domain.InterfaceInfo;
import com.dkd.interfaceinfo.service.IInterfaceInfoService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;

/**
 * 接口信息Controller
 * 
 * @author ruoyi
 * @date 2025-03-03
 */
@RestController
@RequestMapping("/interfaceinfo/info")
public class InterfaceInfoController extends BaseController
{
    @Autowired
    private IInterfaceInfoService interfaceInfoService;

    @Resource
    private SysUserServiceImpl sysUserService;

    @Resource
    private YpApiClient ypApiClient;

    /**
     * 查询接口信息列表
     */
    @PreAuthorize("@ss.hasPermi('interfaceinfo:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(InterfaceInfo interfaceInfo)
    {
        startPage();
        List<InterfaceInfo> list = interfaceInfoService.selectInterfaceInfoList(interfaceInfo);
        return getDataTable(list);
    }

    /**
     * 导出接口信息列表
     */
    @PreAuthorize("@ss.hasPermi('interfaceinfo:info:export')")
    @Log(title = "接口信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InterfaceInfo interfaceInfo)
    {
        List<InterfaceInfo> list = interfaceInfoService.selectInterfaceInfoList(interfaceInfo);
        ExcelUtil<InterfaceInfo> util = new ExcelUtil<InterfaceInfo>(InterfaceInfo.class);
        util.exportExcel(response, list, "接口信息数据");
    }

    /**
     * 获取接口信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('interfaceinfo:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(interfaceInfoService.selectInterfaceInfoById(id));
    }

    /**
     * 新增接口信息
     */
    @PreAuthorize("@ss.hasPermi('interfaceinfo:info:add')")
    @Log(title = "接口信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InterfaceInfo interfaceInfo)
    {
        return toAjax(interfaceInfoService.insertInterfaceInfo(interfaceInfo));
    }

    /**
     * 修改接口信息
     */
    @PreAuthorize("@ss.hasPermi('interfaceinfo:info:edit')")
    @Log(title = "接口信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InterfaceInfo interfaceInfo)
    {
        return toAjax(interfaceInfoService.updateInterfaceInfo(interfaceInfo));
    }

    /**
     * 删除接口信息
     */
    @PreAuthorize("@ss.hasPermi('interfaceinfo:info:remove')")
    @Log(title = "接口信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(interfaceInfoService.deleteInterfaceInfoByIds(ids));
    }

    @PostMapping("/invoke")
    public AjaxResult invoke(@RequestBody InterfaceInfoInvoke interfaceInfoInvoke)
    {
        if (interfaceInfoInvoke == null || interfaceInfoInvoke.getUserRequestParams() == null || interfaceInfoInvoke.getId()<= 0 ){
             return  error("请求参数为空");
        }
        String userRequestParams = interfaceInfoInvoke.getUserRequestParams();
        long id = interfaceInfoInvoke.getId();
        InterfaceInfo interfaceInfo = interfaceInfoService.selectInterfaceInfoById(id);
        if (interfaceInfo == null){
            return  error("接口不存在");
        }
        if (interfaceInfo.getStatus() == 0){
            return  error("接口已关闭");
        }
        Long userId = SecurityUtils.getUserId();

        SysUser sysUser = sysUserService.selectUserById(userId);
        String accesskey = sysUser.getAccesskey();
        String secretkey = sysUser.getSecretkey();
//        String result = ypApiClient.getUserNameByPost();
//        return success(result);
        YpApiClient ypApiClient1 = new YpApiClient(accesskey,secretkey);
        Gson gson = new Gson();
        User user = gson.fromJson(userRequestParams, User.class);

        String result = ypApiClient1.getUserNameByPost(user);

        return AjaxResult.success("操作成功",result);
    }
}
